clear all
set more off, perm
set mem 10000000
set matsize 10000
version 13

************************************************************* 
*** Table and figures for JPE revisions: fuzzy DD results ***
************************************************************* 

** Set file paths
do "$path_code/paths.do"

** Set graph scheme
cd "$path/code/analyze"
set scheme fb, perm

********************************************************************************
********************************************************************************

** Table 6: Fuzzy RD with SHRUG outcomes
{
cap file close tablewrite
use "$results/RDROBUST_outcomes_fuzzy_shrug.dta", clear
append using "$results/RDROBUST_outcomes_fuzzy_secc.dta"
keep if fuzzy_step==1
keep if fuzzyvar=="lights_max2011_hat" | fuzzyvar=="vdp_pwr_h_com_avg_11"
assert ifs=="in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20"
assert fe=="STFEfs*" | fe=="STFEhfs*"
assert kernel=="Triangular"
assert polynomial==1

gen c = .
replace c = 1 if fuzzyvar=="vdp_pwr_h_com_avg_11" & yvar=="secc11_rural_cons_pc" 
replace c = 2 if fuzzyvar=="vdp_pwr_h_com_avg_11" & yvar=="ln_secc11_rural_cons_pc"
replace c = 3 if fuzzyvar=="lights_max2011_hat" & yvar=="secc11_rural_cons_pc" 
replace c = 4 if fuzzyvar=="lights_max2011_hat" & yvar=="ln_secc11_rural_cons_pc"
drop if c==.
assert _N==4
sort c

forvalues c = 1/4 {
		
	local beta`c' = string(beta_robust[`c'],"%9.3f")
	local se`c' = string(se_robust[`c'],"%9.3f")
	if `c'==1 | `c'==3 {
		local beta`c' = string(beta_robust[`c'],"%9.3f")
		local se`c' = string(se_robust[`c'],"%9.3f")	
	}	
	if pval_robust[`c'] <= 0.01 {
		local stars`c' = "$^{***}$"
	}
	else if pval_robust[`c'] <= 0.05 {
		local stars`c' = "$^{**}$"
	}
	else if pval_robust[`c'] <= 0.10 {
		local stars`c' = "$^{*}$"
	}
	else {
		local stars`c' = ""
	}

	if inrange(`c',1,2) {
		local K = 10
	}
	else if inrange(`c',3,4) {
		local K = 2.6
	}
	
	local beta`c'_re =  string(beta_robust[`c']*`K',"%9.3f")
	
	local lci`c' = string(lci_robust[`c']*`K', "%9.3f")
	local uci`c' = string(uci_robust[`c']*`K', "%9.3f")
	if `c'==1  | `c'==3 {
		local lci`c' = string(lci_robust[`c']*`K', "%9.1f")
		local uci`c' = string(uci_robust[`c']*`K', "%9.1f")		
	}
	
	local nobs`c' = string(nobs_left[`c'] + nobs_right[`c'],"%9.0fc")
	local bw`c' = string(bw_lo[`c'], "%9.0f")
	local ymean`c' = string(ymean[`c'],"%9.3f")
	if `c'==1 | `c'==3 {
		local ymean`c' = string(ymean[`c'],"%9.1f")
	}
}	

	
   // MAKE TABLE
file open tablewrite using "$textab/table_rd_fuzzy_shrug.tex", write text replace
 
file write tablewrite "\begin{table}[p]\centering" _n
file write tablewrite "\caption{Fuzzy RD in expenditure per capita, using two endogenous variables}" _n
file write tablewrite "\label{tab:rd_fuzzy_shrug}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\small" _n
file write tablewrite "\begin{tabular}{lccccccc} " _n
file write tablewrite "\hline " _n
file write tablewrite "& \multicolumn{5}{c}{Expenditure per capita (Rs/month)} \\ " _n
file write tablewrite "\vspace{-0.37cm} " _n
file write tablewrite "\\ " _n
file write tablewrite "\cline{2-6} \\ " _n
file write tablewrite "\vspace{-0.80cm} " _n
file write tablewrite "\\ " _n
file write tablewrite "& Levels & Logs && Levels & Logs  \\ " _n
file write tablewrite "[0.1em]" _n
file write tablewrite " & (1) & (2) && (3) & (4) \\ " _n
file write tablewrite "\hline" _n
file write tablewrite "Hours/day of commercial power~~~ & " _n
file write tablewrite "$`beta1'$`stars1' & $`beta2'$`stars2' \\ " _n
file write tablewrite "& $(`se1')$ & $(`se2')$\\" _n
file write tablewrite "[0.5em] " _n
file write tablewrite "LATE for a 10-hour increase & $`beta1_re'$ & $`beta2_re'$ \\ " _n
file write tablewrite "[0.5em] " _n
file write tablewrite "95\% CI for a 10-hour increase~~~~ & \$[`lci1'\$, & \$[`lci2'\$, \\ " _n
file write tablewrite "& \$~`uci1']\$ & \$~`uci2']\$ \\ " _n
file write tablewrite "[1.5em] " _n
file write tablewrite "Units of nighttime brightness  & " _n
file write tablewrite "&& & $`beta3'$`stars3' & $`beta4'$`stars4' \\" _n
file write tablewrite "&& & & $(`se3')$ & $(`se4')$ \\" _n
file write tablewrite "[0.5em] " _n
file write tablewrite "LATE for a 2.6-unit increase && & &  $`beta3_re'$ & $`beta4_re'$ \\ " _n
file write tablewrite "[0.5em] " _n
file write tablewrite "95\% CI for a 2.6-unit increase && & & \$[`lci3'\$, & \$[`lci4'\$, \\ " _n
file write tablewrite "&& & &  \$~`uci3']\$ & \$~`uci4']\$ \\ " _n
file write tablewrite "[1.0em] " _n
file write tablewrite "Mean of dep var (\$<300\$) & `ymean1' & `ymean2' && `ymean3' & `ymean4' \\ " _n
file write tablewrite "Optimal bandwidth & `bw1' & `bw2' && `bw3' & `bw4' \\ " _n
file write tablewrite "Village observations & ~~`nobs1'~~ & ~~`nobs2'~~ && ~~`nobs3'~~ & ~~`nobs4'~~ \\ " _n
file write tablewrite "\hline" _n
file write tablewrite "\end{tabular} " _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- Fuzzy RD robust estimates using two endogenous village-level \`\`treatment'' variables:  " _n
file write tablewrite "2011 average hours per day of commercial power in Columns (1)--(2), and 2011 nighttime brightness in Columns (3)--(4). " _n
file write tablewrite "For hours of commercial power, we scale up by a factor of 10, " _n
file write tablewrite "the median of the 2011 distribution of non-zero hours of  " _n
file write tablewrite "commercial power in the RD bandwidth. " _n
file write tablewrite "For nighttime brightness, we use a scaling factor of 2.6, equal to " _n
file write tablewrite "the interquartile range of the 2011 distribution  " _n
file write tablewrite "of village-level brightness in the RD bandwidth.   " _n
file write tablewrite "Both scaling factors denominate local average treatment effects for  " _n
file write tablewrite "a village moving from the 25th to the 75th percentile of the \`\`treatment'' variable. " _n
file write tablewrite "Outcomes variables are 2011 SHRUG consumption expenditures per capita in levels (Rs/month) and logs. " _n
file write tablewrite "Each fuzzy RD uses a linear polynomial, triangular kernel, MSE-optimal bandwidth, " _n
file write tablewrite "and nearest-neighbor standard errors. Regression samples include within-bandwidth single-habitation " _n
file write tablewrite "villages in RGGVY 10th-Plan districts (i.e.\ the first wave of RGGVY implementation,  " _n
file write tablewrite "for which 300 people is the relevant eligibility cutoff).  " _n
file write tablewrite "Regressions control for pre-2005 nighttime brightness at the village level, and state fixed effects.  " _n
file write tablewrite "Optimal bandwidths are symmetric above and below the 300-person cutoff, and we report means of the dependent  " _n
file write tablewrite "variable for villages below the cutoff. Results are robust to alternative controls, kernels and bandwidth algorithms.  " _n
file write tablewrite "Significance: *** \$p < 0.01\$, ** \$p < 0.05\$, * \$p < 0.10\$." _n
file write tablewrite "}" _n
file write tablewrite "\end{table}" _n

file close tablewrite

}

********************************************************************************
********************************************************************************

** Table A16: Fuzzy RD with SHRUG outcomes, high-intensity RGGVY districts
{
cap file close tablewrite
use "$results/RDROBUST_outcomes_fuzzy_shrug_dpr.dta", clear
append using "$results/RDROBUST_outcomes_fuzzy_secc_dpr.dta"
keep if fuzzy_step==1
keep if dprtag=="hi_vill"
keep if fuzzyvar=="lights_max2011_hat" | fuzzyvar=="vdp_pwr_h_com_avg_11"
assert regexm(ifs,"in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20")
assert fe=="STFEfs*" | fe=="STFEhfs*"
assert kernel=="Triangular"
assert polynomial==1

gen c = .
replace c = 1 if fuzzyvar=="vdp_pwr_h_com_avg_11" & yvar=="secc11_rural_cons_pc" 
replace c = 2 if fuzzyvar=="vdp_pwr_h_com_avg_11" & yvar=="ln_secc11_rural_cons_pc"
replace c = 3 if fuzzyvar=="lights_max2011_hat" & yvar=="secc11_rural_cons_pc" 
replace c = 4 if fuzzyvar=="lights_max2011_hat" & yvar=="ln_secc11_rural_cons_pc"
drop if c==.
assert _N==4
sort c
assert c==_n

preserve
use "$panel/panel_dataset_full.dta", clear
gen in_fs_sample = vplan4<11 & corr_state==1 & sample==1 & sample_h==1 & sing_h==1 & pop_non_zero==1 
sum hpca11_msl_elec if in_fs_sample==1 & inrange(tot_p,150,450) & vd_pwr_d_dom_11==1, detail
di 1/(r(p75) - r(p25))
di r(p75)-r(p25)
restore

forvalues c = 1/4 {
		
	local beta`c' = string(beta_robust[`c'],"%9.3f")
	local se`c' = string(se_robust[`c'],"%9.3f")
	if `c'==1 | `c'==3 | `c'==5 {
		local beta`c' = string(beta_robust[`c'],"%9.3f")
		local se`c' = string(se_robust[`c'],"%9.3f")	
	}	
	if pval_robust[`c'] <= 0.01 {
		local stars`c' = "$^{***}$"
	}
	else if pval_robust[`c'] <= 0.05 {
		local stars`c' = "$^{**}$"
	}
	else if pval_robust[`c'] <= 0.10 {
		local stars`c' = "$^{*}$"
	}
	else {
		local stars`c' = ""
	}

	if inrange(`c',1,2) {
		local K = 10
	}
	else if inrange(`c',3,4) {
		local K = 2.6
	}
	
	local beta`c'_re =  string(beta_robust[`c']*`K',"%9.3f")
	
	local lci`c' = string(lci_robust[`c']*`K', "%9.3f")
	local uci`c' = string(uci_robust[`c']*`K', "%9.3f")
	if `c'==1  | `c'==3 {
		local lci`c' = string(lci_robust[`c']*`K', "%9.1f")
		local uci`c' = string(uci_robust[`c']*`K', "%9.1f")		
	}
	
	local nobs`c' = string(nobs_left[`c'] + nobs_right[`c'],"%9.0fc")
	local bw`c' = string(bw_lo[`c'], "%9.0f")
	local ymean`c' = string(ymean[`c'],"%9.3f")
	if `c'==1 | `c'==3 {
		local ymean`c' = string(ymean[`c'],"%9.1f")
	}
}	

	
   // MAKE TABLE
file open tablewrite using "$textab/table_rd_fuzzy_shrug_hi_intens.tex", write text replace
 
file write tablewrite "\begin{table}[h!]\centering" _n
file write tablewrite "\caption{Fuzzy RD in expenditure per capita, high-intensity districts}" _n
file write tablewrite "\label{tab:rd_fuzzy_shrug_hi_intens}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\small" _n
file write tablewrite "\begin{tabular}{lccccccccc} " _n
file write tablewrite "\hline " _n
file write tablewrite "& \multicolumn{5}{c}{Expenditure per capita (Rs/month)} \\ " _n
file write tablewrite "\vspace{-0.37cm} " _n
file write tablewrite "\\ " _n
file write tablewrite "\cline{2-6} \\ " _n
file write tablewrite "\vspace{-0.8cm} " _n
file write tablewrite "\\ " _n
file write tablewrite "& Levels & Logs && Levels & Logs   \\ " _n
file write tablewrite "[0.1em]" _n
file write tablewrite " & (1) & (2) && (3) & (4)  \\ " _n
file write tablewrite "\hline" _n
file write tablewrite "Hours/day of commercial power~~~ & " _n
file write tablewrite "$`beta1'$`stars1' & $`beta2'$`stars2' \\ " _n
file write tablewrite "& $(`se1')$ & $(`se2')$\\" _n
file write tablewrite "[0.5em] " _n
file write tablewrite "LATE for a 10-hour increase & $`beta1_re'$ & $`beta2_re'$ \\ " _n
file write tablewrite "[0.5em] " _n
file write tablewrite "95\% CI for a 10-hour increase~~~~ & \$[`lci1',`uci1']\$ & \$[`lci2',`uci2']\$ \\ " _n
file write tablewrite "[1.5em] " _n
file write tablewrite "Units of nighttime brightness  & " _n
file write tablewrite "&& & $`beta3'$`stars3' & $`beta4'$`stars4' \\" _n
file write tablewrite "&& & & $(`se3')$ & $(`se4')$ \\" _n
file write tablewrite "[0.5em] " _n
file write tablewrite "LATE for a 2.6-unit increase && & &  $`beta3_re'$ & $`beta4_re'$ \\ " _n
file write tablewrite "[0.5em] " _n
file write tablewrite "95\% CI for a 2.6-unit increase && & & \$[`lci3',`uci3']\$ & \$[`lci4',`uci4']\$ \\ " _n
file write tablewrite "[1.0em] " _n
file write tablewrite "Mean of dep var (\$<300\$) & `ymean1' & `ymean2' && `ymean3' & `ymean4' \\ " _n
file write tablewrite "Optimal bandwidth & `bw1' & `bw2' && `bw3' & `bw4' \\ " _n
file write tablewrite "Village observations & ~~`nobs1'~~ & ~~`nobs2'~~ && ~~`nobs3'~~ & ~~`nobs4'~~  \\ " _n
file write tablewrite "\hline" _n
file write tablewrite "\end{tabular} " _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- " _n
file write tablewrite "Regressions are identical to those in Table \ref*{tab:rd_fuzzy_shrug}, except that we " _n
file write tablewrite "restrict the sample to the subset of RGGVY 10th-Plan districts where the program " _n
file write tablewrite "treated at least 60\% of all villages in the district (per RGGVY district-level " _n
file write tablewrite "implementation data). " _n
file write tablewrite "This reduces the main RD sample from 130 districts in 12 states to 90 districts in 11 states. " _n
file write tablewrite "See notes under Table \ref*{tab:rd_fuzzy_shrug} for further details. " _n
file write tablewrite "Results are robust to alternative kernels and bandwidth algorithms. " _n
file write tablewrite "Significance: *** \$p < 0.01\$, ** \$p < 0.05\$, * \$p < 0.10\$." _n
file write tablewrite "}" _n
file write tablewrite "\end{table}" _n

file close tablewrite

}

********************************************************************************
********************************************************************************

** Figure A13: Histograms for benchmarking changes in endogenoues varaibles 
{
	
use "$panel/panel_dataset_full.dta", clear
drop hpca*

	// Keep villages in RD sample
gen in_fs_sample = vplan4<11 & corr_state==1 & sample==1 & sample_h==1 & sing_h==1 & pop_non_zero==1 
keep if in_fs_sample==1

	// Create lights-difference variable, to identify crazy outliers
gen lights_diff = abs(lights_max2011_hat - lights_max2001_hat)

	// Hours of commercial power per day
twoway (hist vdp_pwr_h_com_avg_11 if in_fs_sample==1 & inrange(tot_p,150,450) & vdp_pwr_d_com_11==1, ///
	start(0) width(1) freq fcolor(navy) lcolor(black)) , ///
	title("Hours/day of commercial power (2011)", color(black) size(large)) ///
	ytitle("Count of in-bandwidth villages" "with any commercial power", size(medlarge)) ///
	xtitle("Hours per day", size(medlarge)) aspect(0.6) ///
	ylabel(0(200)800, nogrid angle(0) labsize(medsmall)) ///
	xlabel(0(4)24, labsize(medsmall))
graph export "$texfig/avg_hours_commercial_power_hist.pdf", replace

sum  vdp_pwr_h_com_avg_11 if in_fs_sample==1 & inrange(tot_p,150,450) & vdp_pwr_d_com_11==1, detail
local n1 = string(r(N),"%9.0fc")
local mean1 = string(r(mean),"%9.1f")
local sd1 = string(r(sd),"%9.1f")
local med1 = string(r(p50),"%9.0f")
local iqr1 = string(r(p75)-r(p25),"%9.0f")
sum  vdp_pwr_h_com_avg_11 if inrange(tot_p,150,450), detail
	
count if in_fs_sample==1 & inrange(tot_p,150,450) & vdp_pwr_d_com_11==1 & vdp_pwr_h_com_avg_11==24
local rN = r(N)	
count if in_fs_sample==1 & inrange(tot_p,150,450) & vdp_pwr_d_com_11==1 
di `rN'/r(N)

count if in_fs_sample==1 & inrange(tot_p,150,450) & vd_pwr_d_all_11==1 & vdp_pwr_h_all_avg_11==24
local rN = r(N)	
count if in_fs_sample==1 & inrange(tot_p,150,450) & vd_pwr_d_all_11==1 
di `rN'/r(N)

count if in_fs_sample==1 & inrange(tot_p,150,450) & vd_pwr_d_dom_11==1 & vdp_pwr_h_dom_avg_11==24
local rN = r(N)	
count if in_fs_sample==1 & inrange(tot_p,150,450) & vd_pwr_d_dom_11==1 
di `rN'/r(N)


	// Nighttime brightness
twoway (hist lights_max2011_hat if in_fs_sample==1 & inrange(tot_p,150,450) & lights_diff<20 & lights_max2011_hat<=30, ///
	start(0) width(1) freq fcolor(navy) lcolor(black)) , ///
	title("Nighttime brightness (2011)", color(black) size(large)) ///
	ytitle("In-bandwidth villages", size(medlarge)) ///
	xtitle("Brightness points", size(medlarge)) aspect(0.6) ///
	ylabel(0(1000)5000, nogrid angle(0) labsize(medsmall)) ///
	xlabel(0(5)30, labsize(medsmall))
graph export "$texfig/2011_max_lights_hist.pdf", replace

sum  lights_max2011_hat if in_fs_sample==1 & inrange(tot_p,150,450) & lights_diff<20 & lights_max2011_hat<=30, detail
local n2 = string(r(N),"%9.0fc")
local mean2 = string(r(mean),"%9.1f")
local sd2 = string(r(sd),"%9.1f")
local iqr2 = string(r(p75)-r(p25),"%9.1f")


   // MAKE FIGURE
file open tablewrite using "$texfig/figure_endog_var_histograms.tex", write text replace
 
file write tablewrite "\begin{figure}[h!]\centering" _n
file write tablewrite "\caption{Histograms of fuzzy RD \`\`treatment'' variables}" _n
file write tablewrite "\label{fig:endog_var_histograms}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "{\includegraphics[width=0.48\textwidth, trim={0mm 7mm 0mm 4mm}, clip]{${texfig_short}/avg_hours_commercial_power_hist.pdf}} " _n
file write tablewrite "{\includegraphics[width=0.48\textwidth, trim={0mm 7mm 0mm 4mm}, clip]{${texfig_short}/2011_max_lights_hist.pdf}} " _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- This figure reports histograms of the  " _n
file write tablewrite "two endogenous \`\`treatment'' variables that we use to estimate fuzzy RDs. " _n
file write tablewrite "The right histogram includes all `n2' villages in our main RD sample with 2001 populations between 150--450, " _n
file write tablewrite "for which the distribution of 2011 nighttime brightness has a mean of `mean2', " _n
file write tablewrite "a standard deviation of `sd2', and an interquartile range of `iqr2' (which we use to scale our fuzzy RD confidence intervals by 2.6). " _n
file write tablewrite "The left histogram uses the same sample, while also conditioning on villages having non-zero commercial power access in 2011. " _n
file write tablewrite "This leaves a sample of `n1' villages. This subsample has a " _n
file write tablewrite "mean of `mean1', a standard deviation of `sd1', a median of `med1', and an interquartile range of `iqr1' " _n
file write tablewrite "hours per day of commercial power supply (we rescale our fuzzy RD confidence intervals by 10). " _n
file write tablewrite "}" _n
file write tablewrite "\end{figure}" _n 

file close tablewrite	
	
}

********************************************************************************
********************************************************************************

** Figure A14: Scatter plots correlating brightness with electric lighting penetration and hours of commercial power
{

use "$panel/panel_dataset_full.dta", clear

	// Create lights-difference variable, to identify crazy outliers and drop them
gen lights_diff = abs(lights_max2011_hat - lights_max2001_hat)
drop if lights_diff>=20 // same threshold used in nighttiem brightness regressions

 	// Integer-ize hours of power
gen hrs_com_11 = ceil(vdp_pwr_h_com_avg_11)	
gen hrs_all_11 = ceil(vdp_pwr_h_all_avg_11)	

	// 10 percentiles of main source of lighting
gen msl_elec_pctile = .
forvalues p = 10(10)100 {
	local p0 = `p'-10 
	replace msl_elec_pctile = `p' if inrange(hpca11_msl_elec,`p0'/100,`p'/100)
}

	// 7 buckets of hours of commercial power
gen hrs_com_11_bucket = .
forvalues b = 4(4)24 {
	replace hrs_com_11_bucket = `b' if vdp_pwr_h_com_avg_11<=`b' & hrs_com_11_bucket==.
}	

	// Summary stats to plot
tabstat vdp_pwr_h_com_avg_11, by(hrs_all_11) s(p5 p10 p25 p50 p75 p90 p95 mean n)
tabstat lights_max2011_hat, by(msl_elec_pctile) s(p5 p10 p25 p50 p75 p90 p95 mean n)

	// Construct lights-MSL scatter plot
gen msl = .
replace msl = _n*10 in 1/10
gen msl_x = msl-5
gen lights_p50 = .
gen lights_p25 = .
gen lights_p75 = .
forvalues msl = 10(10)100 {
	sum lights_max2011_hat if msl_elec_pctile==`msl', detail
	replace lights_p50 = r(p50) if msl==`msl'
	replace lights_p25 = r(p25) if msl==`msl'
	replace lights_p75 = r(p75) if msl==`msl'
}

	// Construct lights-hours scatter plot
gen H = .
replace H = (_n)*4 in 1/6
gen lightsH_p50 = .
gen lightsH_p25 = .
gen lightsH_p75 = .
forvalues H = 4(4)24 {
	sum lights_max2011_hat if hrs_com_11_bucket==`H', detail
	replace lightsH_p50 = r(p50) if H==`H'
	replace lightsH_p25 = r(p25) if H==`H'
	replace lightsH_p75 = r(p75) if H==`H'
}

/*
A 0.25-unit increase in brightness ~ X more hours of commercial power

Bucket	Median	Delta	0.25/Delta*4 = X
1-4		4.487
5-8		4.804	0.317		3.155
9-12	6.133	1.329		0.752
13-16	6.086	-0.047  	-21.27
17-20	6.480	0.394		2.54
21-24	7.024	0.544		1.84

*/

twoway ///
	(rcap lights_p75 lights_p25 msl_x, lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter lights_p50 msl_x, msize(medsmall) mc(midblue) msymbol(O)) ///
	, ///
	ytitle("Median village-level brightness, 2011", size(medsmall)) ///
	xtitle("Percent of households with electric lighting, 2011", size(medsmall)) ///
	title("Brightness vs. electric lighting penetration ", color(black) size(medium)) /// 
	graphregion(lcolor(white)) graphregion(color(white)) plotregion(fcolor(white)) ///
	ylabel(, nogrid angle(0) labsize(medsmall)) ///
	xlabel(0(10)100, labsize(medsmall)) ///
	legend(off) aspect(0.77)
graph export "$texfig/figure_scatter_lights_msl_elec.pdf", replace 	

	
twoway ///
	(rcap lightsH_p75 lightsH_p25 H, lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter lightsH_p50 H, msize(medsmall) mc(midblue) msymbol(O)) ///
	, ///
	ytitle("Median village-level brightness, 2011", size(medsmall)) ///
	xtitle("Hours/day of commercial power, 2011", size(medsmall)) ///
	title("Brightness vs. commercial power access ", color(black) size(medium)) /// 
	graphregion(lcolor(white)) graphregion(color(white)) plotregion(fcolor(white)) ///
	ylabel(, nogrid angle(0) labsize(medsmall)) ///
	xlabel(4 "0-4" 8 "5-8" 12 "9-12" 16 "13-16" 20 "17-20" 24 "21-24", labsize(medsmall)) ///
	legend(off) aspect(0.77)
graph export "$texfig/figure_scatter_lights_hours_com.pdf", replace 	
		
	
   // MAKE FIGURE
file open tablewrite using "$texfig/figure_scatter_lights_correlations.tex", write text replace
 
file write tablewrite "\begin{figure}[h]\centering" _n
file write tablewrite "\caption{Comparing nighttime brightness to village-level Census variables}" _n
file write tablewrite "\label{fig:scatter_lights_correlations}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "{\includegraphics[width=0.49\textwidth, trim={9mm 3mm 9mm 0mm}, clip]{${texfig_short}/figure_scatter_lights_msl_elec.pdf}} " _n
file write tablewrite "{\includegraphics[width=0.49\textwidth, trim={9mm 3mm 9mm 0mm}, clip]{${texfig_short}/figure_scatter_lights_hours_com.pdf}} " _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- Both panels plot median 2011 nighttime brightness " _n
file write tablewrite "at the village level, with whiskers showing the interquartile range. " _n
file write tablewrite "The left panel splits villages by the percent of households with electric lighting (i.e., between " _n
file write tablewrite "0--10\%, 10--20\%, etc.). A 3.2-unit increase in brightness (roughly) corresponds to a " _n
file write tablewrite "100-percentage-point increase in the penetration of residential electric lighting for the median village. " _n
file write tablewrite "The right panel splits villages by hours per day of commercial power access (i.e., between " _n
file write tablewrite "0--4 hours, 5--8 hours, etc.) A 2.5-unit increase in brightness (roughly) corresponds to a " _n
file write tablewrite "24-hour increase in commercial power access for the median village. " _n 
file write tablewrite "}" _n
file write tablewrite "\end{figure}" _n
 
file close tablewrite	


}

****************************************************************** 
******************************************************************

** Table A30: Fuzzy RD with all outcomes in main reduced-form table, using hours of commercial power
{
local K = 10

use "$results/RDROBUST_outcomes_fuzzy_shrug.dta", clear
keep if fuzzy_step==1
keep if fuzzyvar=="vdp_pwr_h_com_avg_11"
assert ifs=="in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20"
assert fe=="STFEfs*"
assert kernel=="Triangular"
assert polynomial==1

gen c = .
replace c = 1 if yvar=="secc11_rural_cons_pc" 
replace c = 2 if yvar=="ln_secc11_rural_cons_pc"
replace c = 3 if yvar=="secc11_inc_cultiv_share" 
keep if c!=.
unique c
assert r(unique)==r(N) & r(unique)==3

forvalues c = 1/3 {
		
	local beta`c' = string(beta_robust[`c'],"%9.3f")
	local se`c' = string(se_robust[`c'],"%9.3f")
	if `c'==1 {
		local beta`c' = string(beta_robust[`c'],"%9.1f")
		local se`c' = string(se_robust[`c'],"%9.1f")
	}	
	if pval_robust[`c'] <= 0.01 {
		local stars`c' = "$^{***}$"
	}
	else if pval_robust[`c'] <= 0.05 {
		local stars`c' = "$^{**}$"
	}
	else if pval_robust[`c'] <= 0.10 {
		local stars`c' = "$^{*}$"
	}
	else {
		local stars`c' = ""
	}

	local lci`c' = string(`K'*lci_robust[`c'], "%9.3f")
	local uci`c' = string(`K'*uci_robust[`c'], "%9.3f")
	if `c'==1 {
		local lci`c' = string(`K'*lci_robust[`c'], "%9.1f")
		local uci`c' = string(`K'*uci_robust[`c'], "%9.1f")
	}
	
	local nobs`c' = string(nobs_left[`c'] + nobs_right[`c'],"%9.0fc")
	local bw`c' = string(bw_lo[`c'], "%9.0f")
	local ymean`c' = string(ymean[`c'],"%9.3f")
	if `c'==1 {
		local ymean`c' = string(ymean[`c'],"%9.1f")
	}
}	

assert bw_lo==bw_hi
sum bw_lo if !inlist(c,1,2)
local bw_min = r(min)
local bw_max = r(max)


***

use "$results/RDROBUST_outcomes_fuzzy_secc.dta", clear
keep if fuzzy_step==1
keep if fuzzyvar=="vdp_pwr_h_com_avg_11"
assert ifs=="in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20"
assert fe=="STFEhfs*"
assert kernel=="Triangular"
assert polynomial==1

gen c = .
replace c = 4 if yvar=="hh_ratio" 
replace c = 5 if yvar=="pct_hh_mnth_inc_5_10"
replace c = 6 if yvar=="pct_hh_salaried_job"
keep if c!=.
unique c
assert r(unique)==r(N) & r(unique)==3
set obs 6
gen blank = c==.
gsort -blank c 
assert blank | c==_n

forvalues c = 4/6 {
		
	local beta`c' = string(beta_robust[`c'],"%9.3f")
	local se`c' = string(se_robust[`c'],"%9.3f")
	if pval_robust[`c'] <= 0.01 {
		local stars`c' = "$^{***}$"
	}
	else if pval_robust[`c'] <= 0.05 {
		local stars`c' = "$^{**}$"
	}
	else if pval_robust[`c'] <= 0.10 {
		local stars`c' = "$^{*}$"
	}
	else {
		local stars`c' = ""
	}
	local lci`c' = string(`K'*lci_robust[`c'], "%9.3f")
	local uci`c' = string(`K'*uci_robust[`c'], "%9.3f")

	local nobs`c' = string(nobs_left[`c'] + nobs_right[`c'],"%9.0fc")
	local bw`c' = string(bw_lo[`c'], "%9.0f")
	local ymean`c' = string(ymean[`c'],"%9.3f")
}	

assert bw_lo==bw_hi
sum bw_lo if !inlist(c,4)
local bw_min = min(r(min),`bw_min')
local bw_max = max(r(max),`bw_max')

		
***	

use "$results/RDROBUST_outcomes_fuzzy_census.dta", clear
keep if fuzzy_step==1
keep if fuzzyvar=="vdp_pwr_h_com_avg_11"
assert ifs=="in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20"
assert fe=="STFEfs*"
assert kernel=="Triangular"
assert polynomial==1

gen c = .
replace c = 7 if yvar=="tot_p11" 
replace c = 8 if yvar=="pct_06_11"
replace c = 9 if yvar=="hpca11_h_size_avg" 
replace c = 10 if yvar=="work_pooled_ag_p_11"
replace c = 11 if yvar=="work_pooled_ag_m_11" 
replace c = 12 if yvar=="work_pooled_ag_f_11"
replace c = 13 if yvar=="work_pooled_ot_p_11"
replace c = 14 if yvar=="work_pooled_ot_m_11" 
replace c = 15 if yvar=="work_pooled_ot_f_11"
keep if c!=.
unique c
assert r(unique)==r(N) & r(unique)==9
set obs 15
gen blank = c==.
gsort -blank c 
assert blank | c==_n

forvalues c = 7/15 {
		
	local beta`c' = string(beta_robust[`c'],"%9.3f")
	local se`c' = string(se_robust[`c'],"%9.3f")
	if pval_robust[`c'] <= 0.01 {
		local stars`c' = "$^{***}$"
	}
	else if pval_robust[`c'] <= 0.05 {
		local stars`c' = "$^{**}$"
	}
	else if pval_robust[`c'] <= 0.10 {
		local stars`c' = "$^{*}$"
	}
	else {
		local stars`c' = ""
	}
	local lci`c' = string(`K'*lci_robust[`c'], "%9.3f")
	local uci`c' = string(`K'*uci_robust[`c'], "%9.3f")

	local nobs`c' = string(nobs_left[`c'] + nobs_right[`c'],"%9.0fc")
	local bw`c' = string(bw_lo[`c'], "%9.0f")
	local ymean`c' = string(ymean[`c'],"%9.3f")
}	

assert bw_lo==bw_hi
sum bw_lo
local bw_min = min(r(min),`bw_min')
local bw_max = max(r(max),`bw_max')

	
***	

use "$results/RDROBUST_outcomes_fuzzy_ec.dta", clear
keep if fuzzy_step==1
keep if fuzzyvar=="vdp_pwr_h_com_avg_11"
assert ifs=="in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20"
assert fe=="STFEfs*"
assert kernel=="Triangular"
assert polynomial==1

gen c = .
replace c = 16 if yvar=="ec_firms2013" 
replace c = 17 if yvar=="ec_employees2013"
keep if c!=.
unique c
assert r(unique)==r(N) & r(unique)==2
set obs 17
gen blank = c==.
gsort -blank c 
assert blank | c==_n

forvalues c = 16/17 {
		
	local beta`c' = string(beta_robust[`c'],"%9.3f")
	local se`c' = string(se_robust[`c'],"%9.3f")
	if pval_robust[`c'] <= 0.01 {
		local stars`c' = "$^{***}$"
	}
	else if pval_robust[`c'] <= 0.05 {
		local stars`c' = "$^{**}$"
	}
	else if pval_robust[`c'] <= 0.10 {
		local stars`c' = "$^{*}$"
	}
	else {
		local stars`c' = ""
	}
	local lci`c' = string(`K'*lci_robust[`c'], "%9.3f")
	local uci`c' = string(`K'*uci_robust[`c'], "%9.3f")

	local nobs`c' = string(nobs_left[`c'] + nobs_right[`c'],"%9.0fc")
	local bw`c' = string(bw_lo[`c'], "%9.0f")
	local ymean`c' = string(ymean[`c'],"%9.3f")
}	

assert bw_lo==bw_hi
sum bw_lo
local bw_min = min(r(min),`bw_min')
local bw_max = max(r(max),`bw_max')

	
***	

use "$results/RDROBUST_outcomes_fuzzy_schools.dta", clear
keep if fuzzy_step==1
keep if fuzzyvar=="vdp_pwr_h_com_avg_11"
assert ifs=="in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20"
assert fe=="STFEfs*"
assert kernel=="Triangular"
assert polynomial==1

gen c = .
replace c = 18 if yvar=="g15_enroll_all2014" 
replace c = 19 if yvar=="g68_enroll_all2014"
replace c = 20 if yvar=="tot_pass60_prev452014" 
replace c = 21 if yvar=="tot_pass60_prev782014"
keep if c!=.
unique c
assert r(unique)==r(N) & r(unique)==4
set obs 21
gen blank = c==.
gsort -blank c 
assert blank | c==_n

forvalues c = 18/21 {
		
	local beta`c' = string(beta_robust[`c'],"%9.3f")
	local se`c' = string(se_robust[`c'],"%9.3f")
	if pval_robust[`c'] <= 0.01 {
		local stars`c' = "$^{***}$"
	}
	else if pval_robust[`c'] <= 0.05 {
		local stars`c' = "$^{**}$"
	}
	else if pval_robust[`c'] <= 0.10 {
		local stars`c' = "$^{*}$"
	}
	else {
		local stars`c' = ""
	}
	local lci`c' = string(`K'*lci_robust[`c'], "%9.3f")
	local uci`c' = string(`K'*uci_robust[`c'], "%9.3f")

	local nobs`c' = string(nobs_left[`c'] + nobs_right[`c'],"%9.0fc")
	local bw`c' = string(bw_lo[`c'], "%9.0f")
	local ymean`c' = string(ymean[`c'],"%9.3f")
}	

assert bw_lo==bw_hi
sum bw_lo
local bw_min = min(r(min),`bw_min')
local bw_max = max(r(max),`bw_max')

local bw_min = string(floor(`bw_min'),"%9.0f")
local bw_max = string(floor(`bw_max'),"%9.0f")	
	
***	

   // MAKE TABLE
cap file close tablewrite
file open tablewrite using "$textab/table_rd_outcomes_fuzzy_hours.tex", write text replace
 
file write tablewrite "\begin{table}[h]\centering" _n
file write tablewrite "\caption{Village-level fuzzy RD outcomes -- hours of commercial power}" _n
file write tablewrite "\label{tab:rd_outcomes_fuzzy_hours}" _n
file write tablewrite "\vspace{-3mm}" _n
file write tablewrite "\small" _n
file write tablewrite "\begin{tabular}{lccccccc} " _n
file write tablewrite "\hline" _n
file write tablewrite "& RD estimate & Std error & 95\% CI \$\times \, 10\$ & Mean \$ Y_v\$ \\" _n
file write tablewrite "\vspace{-0.40cm}" _n
file write tablewrite "\\" _n
file write tablewrite "\cline{2-5} \\" _n
file write tablewrite "\vspace{-0.85cm}" _n
file write tablewrite "\\" _n
file write tablewrite "\multicolumn{2}{l}{\textbf{A.} \underline{Consumption and income (2011)}} \\ " _n
file write tablewrite "[0.15em] " _n
file write tablewrite "~~Share HH with poverty indicator  & $`beta4'$`stars4' & $(`se4')$ & $[`lci4',`uci4']$ & `ymean4' \\" _n
file write tablewrite "~~Share HH rely on cultivation income & $`beta3'$`stars3' & $(`se3')$ & $[`lci3',`uci3']$ & `ymean3' \\" _n
file write tablewrite "~~Share HH earning \$>\$ Rs 5k/mth & $`beta5'$`stars5' & $(`se5')$ & $[`lci5',`uci5']$ & `ymean5' \\" _n
file write tablewrite "~~Share HH with salaried job       & $`beta6'$`stars6' & $(`se6')$ & $[`lci6',`uci6']$ & `ymean6' \\" _n
file write tablewrite "[0.8em] " _n
file write tablewrite "\multicolumn{2}{l}{\textbf{B.} \underline{Village demographics (2011)}} \\ " _n
file write tablewrite "[0.15em] " _n
file write tablewrite "~~Population  & $`beta7'$`stars7' & $(`se7')$ & $[`lci7',`uci7']$ & `ymean7' \\" _n
file write tablewrite "~~Share population age 0--6 & $`beta8'$`stars8' & $(`se8')$ & $[`lci8',`uci8']$ & `ymean8' \\" _n
file write tablewrite "~~Average household size    & $`beta9'$`stars9' & $(`se9')$ & $[`lci9',`uci9']$ & `ymean9' \\" _n
file write tablewrite "[0.8em] " _n
file write tablewrite "\multicolumn{2}{l}{\textbf{C.} \underline{Workers as share of population (2011)}} \\ " _n
file write tablewrite "[0.15em] " _n
file write tablewrite "~~Ag workers, total      & $`beta10'$`stars10' & $(`se10')$ & $[`lci10',`uci10']$ & `ymean10' \\" _n
file write tablewrite "~~Ag workers, male       & $`beta11'$`stars11' & $(`se11')$ & $[`lci11',`uci11']$ & `ymean11' \\" _n
file write tablewrite "~~Ag workers, female     & $`beta12'$`stars12' & $(`se12')$ & $[`lci12',`uci12']$ & `ymean12' \\" _n
file write tablewrite "~~Non-ag workers, total  & $`beta13'$`stars13' & $(`se13')$ & $[`lci13',`uci13']$ & `ymean13' \\" _n
file write tablewrite "~~Non-ag workers, male   & $`beta14'$`stars14' & $(`se14')$ & $[`lci14',`uci14']$ & `ymean14' \\" _n
file write tablewrite "~~Non-ag workers, female & $`beta15'$`stars15' & $(`se15')$ & $[`lci15',`uci15']$ & `ymean15' \\" _n
file write tablewrite "[0.8em] " _n
file write tablewrite "\multicolumn{2}{l}{\textbf{D.} \underline{Firm outcomes (2013)}} \\ " _n
file write tablewrite "[0.15em] " _n
file write tablewrite "~~Number of firms          & $`beta16'$`stars16' & $(`se16')$ & $[`lci16',`uci16']$ & `ymean16' \\" _n
file write tablewrite "~~Number of firm employees & $`beta17'$`stars17' & $(`se17')$ & $[`lci17',`uci17']$ & `ymean17' \\" _n
file write tablewrite "[0.8em] " _n
file write tablewrite "\multicolumn{2}{l}{\textbf{E.} \underline{School outcomes (2014--15 school year)}~~} \\ " _n
file write tablewrite "[0.15em] " _n
file write tablewrite "~~\# students enrolled, grades 1--5~~~~  & $`beta18'$`stars18' & $(`se18')$ & $[`lci18',`uci18']$ & `ymean18' \\" _n
file write tablewrite "~~\# students enrolled, grades 6--8  & $`beta19'$`stars19' & $(`se19')$ & $[`lci19',`uci19']$ & `ymean19' \\" _n
file write tablewrite "~~\# students passed, grades 4--5    & $`beta20'$`stars20' & $(`se20')$ & $[`lci20',`uci20']$ & `ymean20' \\" _n
file write tablewrite "~~\# students passed, grades 7--8    & $`beta21'$`stars21' & $(`se21')$ & $[`lci21',`uci21']$ & `ymean21' \\" _n
file write tablewrite "\hline" _n
file write tablewrite "\end{tabular} " _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- This table reports fuzzy RD result for the outcomes in Table \ref*{tab:rd_outcomes}, " _n
file write tablewrite "using 2011 hours of commercial power access as the endogenous \`\`treatment'' variable. " _n
file write tablewrite "We omit expenditure outcomes, reported in Columns (1)--(2) of Table \ref*{tab:rd_fuzzy_shrug}." _n
file write tablewrite "We apply a scaling factor of 10 to the 95\% confidence intervals. " _n
file write tablewrite "See notes under Table \ref*{tab:rd_fuzzy_shrug} for further details. " _n
file write tablewrite "Optimal bandwidths in the table range from `bw_min' to `bw_max' above/below 300 people. " _n
file write tablewrite "Results are broadly robust to alternative controls, kernels, bandwidth algorithms, and standard errors. " _n
file write tablewrite "Significance: *** \$p < 0.01\$, ** \$p < 0.05\$, * \$p < 0.10\$." _n
file write tablewrite "}" _n
file write tablewrite "\end{table}" _n

file close tablewrite


}

********************************************************************************
********************************************************************************

** Table A31: Fuzzy RD with all outcomes in main reduced-form table,  using brightness
{
local K = 2.6

use "$results/RDROBUST_outcomes_fuzzy_shrug.dta", clear
keep if fuzzy_step==1
keep if fuzzyvar=="lights_max2011_hat"
assert ifs=="in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20"
assert fe=="STFEfs*"
assert kernel=="Triangular"
assert polynomial==1

gen c = .
replace c = 1 if yvar=="secc11_rural_cons_pc" 
replace c = 2 if yvar=="ln_secc11_rural_cons_pc"
replace c = 3 if yvar=="secc11_inc_cultiv_share" 
keep if c!=.
unique c
assert r(unique)==r(N) & r(unique)==3

forvalues c = 1/3 {
		
	local beta`c' = string(beta_robust[`c'],"%9.3f")
	local se`c' = string(se_robust[`c'],"%9.3f")
	if `c'==1 {
		local beta`c' = string(beta_robust[`c'],"%9.1f")
		local se`c' = string(se_robust[`c'],"%9.1f")
	}	
	if pval_robust[`c'] <= 0.01 {
		local stars`c' = "$^{***}$"
	}
	else if pval_robust[`c'] <= 0.05 {
		local stars`c' = "$^{**}$"
	}
	else if pval_robust[`c'] <= 0.10 {
		local stars`c' = "$^{*}$"
	}
	else {
		local stars`c' = ""
	}

	local lci`c' = string(`K'*lci_robust[`c'], "%9.3f")
	local uci`c' = string(`K'*uci_robust[`c'], "%9.3f")
	if `c'==1 {
		local lci`c' = string(`K'*lci_robust[`c'], "%9.1f")
		local uci`c' = string(`K'*uci_robust[`c'], "%9.1f")
	}
	
	local nobs`c' = string(nobs_left[`c'] + nobs_right[`c'],"%9.0fc")
	local bw`c' = string(bw_lo[`c'], "%9.0f")
	local ymean`c' = string(ymean[`c'],"%9.3f")
	if `c'==1 {
		local ymean`c' = string(ymean[`c'],"%9.1f")
	}
}	

assert bw_lo==bw_hi
sum bw_lo if !inlist(c,1,2)
local bw_min = r(min)
local bw_max = r(max)


***

use "$results/RDROBUST_outcomes_fuzzy_secc.dta", clear
keep if fuzzy_step==1
keep if fuzzyvar=="lights_max2011_hat"
assert ifs=="in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20"
assert fe=="STFEfs*"
assert kernel=="Triangular"
assert polynomial==1

gen c = .
replace c = 4 if yvar=="hh_ratio" 
replace c = 5 if yvar=="pct_hh_mnth_inc_5_10"
replace c = 6 if yvar=="pct_hh_salaried_job"
keep if c!=.
unique c
assert r(unique)==r(N) & r(unique)==3
set obs 6
gen blank = c==.
gsort -blank c 
assert blank | c==_n

forvalues c = 4/6 {
		
	local beta`c' = string(beta_robust[`c'],"%9.3f")
	local se`c' = string(se_robust[`c'],"%9.3f")
	if pval_robust[`c'] <= 0.01 {
		local stars`c' = "$^{***}$"
	}
	else if pval_robust[`c'] <= 0.05 {
		local stars`c' = "$^{**}$"
	}
	else if pval_robust[`c'] <= 0.10 {
		local stars`c' = "$^{*}$"
	}
	else {
		local stars`c' = ""
	}
	local lci`c' = string(`K'*lci_robust[`c'], "%9.3f")
	local uci`c' = string(`K'*uci_robust[`c'], "%9.3f")

	local nobs`c' = string(nobs_left[`c'] + nobs_right[`c'],"%9.0fc")
	local bw`c' = string(bw_lo[`c'], "%9.0f")
	local ymean`c' = string(ymean[`c'],"%9.3f")
}	

assert bw_lo==bw_hi
sum bw_lo if !inlist(c,4)
local bw_min = min(r(min),`bw_min')
local bw_max = max(r(max),`bw_max')

		
***	

use "$results/RDROBUST_outcomes_fuzzy_census.dta", clear
keep if fuzzy_step==1
keep if fuzzyvar=="lights_max2011_hat"
assert ifs=="in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20"
assert fe=="STFEfs*"
assert kernel=="Triangular"
assert polynomial==1

gen c = .
replace c = 7 if yvar=="tot_p11" 
replace c = 8 if yvar=="pct_06_11"
replace c = 9 if yvar=="hpca11_h_size_avg" 
replace c = 10 if yvar=="work_pooled_ag_p_11"
replace c = 11 if yvar=="work_pooled_ag_m_11" 
replace c = 12 if yvar=="work_pooled_ag_f_11"
replace c = 13 if yvar=="work_pooled_ot_p_11"
replace c = 14 if yvar=="work_pooled_ot_m_11" 
replace c = 15 if yvar=="work_pooled_ot_f_11"
keep if c!=.
unique c
assert r(unique)==r(N) & r(unique)==9
set obs 15
gen blank = c==.
gsort -blank c 
assert blank | c==_n

forvalues c = 7/15 {
		
	local beta`c' = string(beta_robust[`c'],"%9.3f")
	local se`c' = string(se_robust[`c'],"%9.3f")
	if pval_robust[`c'] <= 0.01 {
		local stars`c' = "$^{***}$"
	}
	else if pval_robust[`c'] <= 0.05 {
		local stars`c' = "$^{**}$"
	}
	else if pval_robust[`c'] <= 0.10 {
		local stars`c' = "$^{*}$"
	}
	else {
		local stars`c' = ""
	}
	local lci`c' = string(`K'*lci_robust[`c'], "%9.3f")
	local uci`c' = string(`K'*uci_robust[`c'], "%9.3f")

	local nobs`c' = string(nobs_left[`c'] + nobs_right[`c'],"%9.0fc")
	local bw`c' = string(bw_lo[`c'], "%9.0f")
	local ymean`c' = string(ymean[`c'],"%9.3f")
}	

assert bw_lo==bw_hi
sum bw_lo
local bw_min = min(r(min),`bw_min')
local bw_max = max(r(max),`bw_max')
	
***	

use "$results/RDROBUST_outcomes_fuzzy_ec.dta", clear
keep if fuzzy_step==1
keep if fuzzyvar=="lights_max2011_hat"
assert ifs=="in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20"
assert fe=="STFEfs*"
assert kernel=="Triangular"
assert polynomial==1

gen c = .
replace c = 16 if yvar=="ec_firms2013" 
replace c = 17 if yvar=="ec_employees2013"
keep if c!=.
unique c
assert r(unique)==r(N) & r(unique)==2
set obs 17
gen blank = c==.
gsort -blank c 
assert blank | c==_n

forvalues c = 16/17 {
		
	local beta`c' = string(beta_robust[`c'],"%9.3f")
	local se`c' = string(se_robust[`c'],"%9.3f")
	if pval_robust[`c'] <= 0.01 {
		local stars`c' = "$^{***}$"
	}
	else if pval_robust[`c'] <= 0.05 {
		local stars`c' = "$^{**}$"
	}
	else if pval_robust[`c'] <= 0.10 {
		local stars`c' = "$^{*}$"
	}
	else {
		local stars`c' = ""
	}
	local lci`c' = string(`K'*lci_robust[`c'], "%9.3f")
	local uci`c' = string(`K'*uci_robust[`c'], "%9.3f")

	local nobs`c' = string(nobs_left[`c'] + nobs_right[`c'],"%9.0fc")
	local bw`c' = string(bw_lo[`c'], "%9.0f")
	local ymean`c' = string(ymean[`c'],"%9.3f")
}	
	
assert bw_lo==bw_hi
sum bw_lo
local bw_min = min(r(min),`bw_min')
local bw_max = max(r(max),`bw_max')

	
***	

use "$results/RDROBUST_outcomes_fuzzy_schools.dta", clear
keep if fuzzy_step==1
keep if fuzzyvar=="lights_max2011_hat"
assert ifs=="in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20"
assert fe=="STFEfs*"
assert kernel=="Triangular"
assert polynomial==1

gen c = .
replace c = 18 if yvar=="g15_enroll_all2014" 
replace c = 19 if yvar=="g68_enroll_all2014"
replace c = 20 if yvar=="tot_pass60_prev452014" 
replace c = 21 if yvar=="tot_pass60_prev782014"
keep if c!=.
unique c
assert r(unique)==r(N) & r(unique)==4
set obs 21
gen blank = c==.
gsort -blank c 
assert blank | c==_n

forvalues c = 18/21 {
		
	local beta`c' = string(beta_robust[`c'],"%9.3f")
	local se`c' = string(se_robust[`c'],"%9.3f")
	if pval_robust[`c'] <= 0.01 {
		local stars`c' = "$^{***}$"
	}
	else if pval_robust[`c'] <= 0.05 {
		local stars`c' = "$^{**}$"
	}
	else if pval_robust[`c'] <= 0.10 {
		local stars`c' = "$^{*}$"
	}
	else {
		local stars`c' = ""
	}
	local lci`c' = string(`K'*lci_robust[`c'], "%9.3f")
	local uci`c' = string(`K'*uci_robust[`c'], "%9.3f")

	local nobs`c' = string(nobs_left[`c'] + nobs_right[`c'],"%9.0fc")
	local bw`c' = string(bw_lo[`c'], "%9.0f")
	local ymean`c' = string(ymean[`c'],"%9.3f")
}	

assert bw_lo==bw_hi
sum bw_lo
local bw_min = min(r(min),`bw_min')
local bw_max = max(r(max),`bw_max')

local bw_min = string(floor(`bw_min'),"%9.0f")
local bw_max = string(floor(`bw_max'),"%9.0f")	
		

***	

   // MAKE TABLE
cap file close tablewrite
file open tablewrite using "$textab/table_rd_outcomes_fuzzy_lights.tex", write text replace
 
file write tablewrite "\begin{table}[p]\centering" _n
file write tablewrite "\caption{Village-level fuzzy RD outcomes -- nighttime brightness}" _n
file write tablewrite "\label{tab:rd_outcomes_fuzzy_lights}" _n
file write tablewrite "\vspace{-3mm}" _n
file write tablewrite "\small" _n
file write tablewrite "\begin{tabular}{lccccccc} " _n
file write tablewrite "\hline" _n
file write tablewrite "& RD estimate & Std error & 95\% CI \$\times \, 2.6\$ & Mean \$ Y_v\$ \\" _n
file write tablewrite "\vspace{-0.40cm}" _n
file write tablewrite "\\" _n
file write tablewrite "\cline{2-5} \\" _n
file write tablewrite "\vspace{-0.85cm}" _n
file write tablewrite "\\" _n
file write tablewrite "\multicolumn{2}{l}{\textbf{A.} \underline{Consumption and income (2011)}} \\ " _n
file write tablewrite "[0.15em] " _n
file write tablewrite "~~Share HH with poverty indicator  & $`beta4'$`stars4' & $(`se4')$ & $[`lci4',`uci4']$ & `ymean4' \\" _n
file write tablewrite "~~Share HH rely on cultivation income & $`beta3'$`stars3' & $(`se3')$ & $[`lci3',`uci3']$ & `ymean3' \\" _n
file write tablewrite "~~Share HH earning \$>\$ Rs 5k/mth & $`beta5'$`stars5' & $(`se5')$ & $[`lci5',`uci5']$ & `ymean5' \\" _n
file write tablewrite "~~Share HH with salaried job       & $`beta6'$`stars6' & $(`se6')$ & $[`lci6',`uci6']$ & `ymean6' \\" _n
file write tablewrite "[0.8em] " _n
file write tablewrite "\multicolumn{2}{l}{\textbf{B.} \underline{Village demographics (2011)}} \\ " _n
file write tablewrite "[0.15em] " _n
file write tablewrite "~~Population  & $`beta7'$`stars7' & $(`se7')$ & $[`lci7',`uci7']$ & `ymean7' \\" _n
file write tablewrite "~~Share population age 0--6 & $`beta8'$`stars8' & $(`se8')$ & $[`lci8',`uci8']$ & `ymean8' \\" _n
file write tablewrite "~~Average household size    & $`beta9'$`stars9' & $(`se9')$ & $[`lci9',`uci9']$ & `ymean9' \\" _n
file write tablewrite "[0.8em] " _n
file write tablewrite "\multicolumn{2}{l}{\textbf{C.} \underline{Workers as share of population (2011)}} \\ " _n
file write tablewrite "[0.15em] " _n
file write tablewrite "~~Ag workers, total      & $`beta10'$`stars10' & $(`se10')$ & $[`lci10',`uci10']$ & `ymean10' \\" _n
file write tablewrite "~~Ag workers, male       & $`beta11'$`stars11' & $(`se11')$ & $[`lci11',`uci11']$ & `ymean11' \\" _n
file write tablewrite "~~Ag workers, female     & $`beta12'$`stars12' & $(`se12')$ & $[`lci12',`uci12']$ & `ymean12' \\" _n
file write tablewrite "~~Non-ag workers, total  & $`beta13'$`stars13' & $(`se13')$ & $[`lci13',`uci13']$ & `ymean13' \\" _n
file write tablewrite "~~Non-ag workers, male   & $`beta14'$`stars14' & $(`se14')$ & $[`lci14',`uci14']$ & `ymean14' \\" _n
file write tablewrite "~~Non-ag workers, female & $`beta15'$`stars15' & $(`se15')$ & $[`lci15',`uci15']$ & `ymean15' \\" _n
file write tablewrite "[0.8em] " _n
file write tablewrite "\multicolumn{2}{l}{\textbf{D.} \underline{Firm outcomes (2013)}} \\ " _n
file write tablewrite "[0.15em] " _n
file write tablewrite "~~Number of firms          & $`beta16'$`stars16' & $(`se16')$ & $[`lci16',`uci16']$ & `ymean16' \\" _n
file write tablewrite "~~Number of firm employees & $`beta17'$`stars17' & $(`se17')$ & $[`lci17',`uci17']$ & `ymean17' \\" _n
file write tablewrite "[0.8em] " _n
file write tablewrite "\multicolumn{2}{l}{\textbf{E.} \underline{School outcomes (2014--15 school year)}~~} \\ " _n
file write tablewrite "[0.15em] " _n
file write tablewrite "~~\# students enrolled, grades 1--5~~~~  & $`beta18'$`stars18' & $(`se18')$ & $[`lci18',`uci18']$ & `ymean18' \\" _n
file write tablewrite "~~\# students enrolled, grades 6--8  & $`beta19'$`stars19' & $(`se19')$ & $[`lci19',`uci19']$ & `ymean19' \\" _n
file write tablewrite "~~\# students passed, grades 4--5    & $`beta20'$`stars20' & $(`se20')$ & $[`lci20',`uci20']$ & `ymean20' \\" _n
file write tablewrite "~~\# students passed, grades 7--8    & $`beta21'$`stars21' & $(`se21')$ & $[`lci21',`uci21']$ & `ymean21' \\" _n
file write tablewrite "\hline" _n
file write tablewrite "\end{tabular} " _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- This table reports fuzzy RD result for the outcomes in Table \ref*{tab:rd_outcomes}, " _n
file write tablewrite "using 2011 nighttime brightness as the endogenous \`\`treatment'' variable. " _n
file write tablewrite "We omit expenditure outcomes, reported in Columns (3)--(4) of Table \ref*{tab:rd_fuzzy_shrug}." _n
file write tablewrite "We apply a scaling factor of 2.6 to the 95\% confidence intervals. " _n
file write tablewrite "See notes under Table \ref*{tab:rd_fuzzy_shrug} for further details. " _n
file write tablewrite "Optimal bandwidths in the table range from `bw_min' to `bw_max' above/below 300 people. " _n
file write tablewrite "Results are broadly robust to alternative controls, kernels, bandwidth algorithms, and standard errors. " _n
file write tablewrite "Significance: *** \$p < 0.01\$, ** \$p < 0.05\$, * \$p < 0.10\$." _n
file write tablewrite "}" _n
file write tablewrite "\end{table}" _n

file close tablewrite


}

********************************************************************************
********************************************************************************

** Figure B8: Fuzzy RD sensitivities with SHRUG outcomes
{

use "$results/RDROBUST_outcomes_fuzzy_shrug.dta", clear
keep if fuzzyvar=="lights_max2011_hat" | fuzzyvar=="vdp_pwr_h_com_avg_11"
keep if yvar=="secc11_rural_cons_pc"
keep if polynomial==1
keep if word(ifs,1)=="in_fs_sample==1"

gen c = .
replace c = 1 if ifs=="in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20" & fuzzy_step==1
replace c = 2 if kernel=="Epanechnikov" 
replace c = 3 if kernel=="Uniform"
replace c = 4 if bwmethod=="cerrd"
replace c = 5 if vce=="vce(cluster stdt)"
replace c = 6 if fe==""
replace c = 7 if fe=="DTFEfs*"
replace c = 8 if ftag=="popmismatch"
keep if c!=.
unique c
assert r(unique)==8 & r(N)==16
replace c = c - 0.07 if fuzzyvar=="vdp_pwr_h_com_avg_11"
replace c = c + 0.07 if fuzzyvar=="lights_max2011_hat"

foreach v of varlist beta_robust lci_robust uci_robust {
	replace `v' = `v'*10 if fuzzyvar=="vdp_pwr_h_com_avg_11"
	replace `v' = `v'*2.6 if fuzzyvar=="lights_max2011_hat"
}

twoway ///
	(rspike uci_robust lci_robust c if inrange(c,0.5,1.5) & fuzzyvar=="vdp_pwr_h_com_avg_11", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust c if inrange(c,0.5,1.5) & fuzzyvar=="vdp_pwr_h_com_avg_11", msize(small) mc(midblue) msymbol(O)) ///
	(rspike uci_robust lci_robust c if c>1.5 & fuzzyvar=="vdp_pwr_h_com_avg_11", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust c if c>1.5 & fuzzyvar=="vdp_pwr_h_com_avg_11", msize(small) mlc(navy) mfc(white) msymbol(O)) ///
	(rspike uci_robust lci_robust c if inrange(c,0.5,1.5) & fuzzyvar=="lights_max2011_hat", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust c if inrange(c,0.5,1.5) & fuzzyvar=="lights_max2011_hat", msize(small) mc(midblue) msymbol(T)) ///
	(rspike uci_robust lci_robust c if c>1.5 & fuzzyvar=="lights_max2011_hat", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust c if c>1.5 & fuzzyvar=="lights_max2011_hat", msize(small) mlc(navy) mfc(white) msymbol(T)) ///
	, ///
	yline(0, lcolor(black) lw(thin)) /// 
	ytitle("Scaled-up RD coeff (95% CI)", size(vsmall)) yscale(titlegap(*-2)) xtitle("") ///
	title("Outcome: 2011 expenditure per capita (Rs/month)", color(black) size(vsmall)) /// 
	graphregion(lcolor(white)) graphregion(color(white)) plotregion(fcolor(white)) ///
	ylabel(,nogrid angle(0) labsize(vsmall)) /// yscale(range(-0.125 0.4)) ///
	xlabel( 1 `" "Preferred" "'  ///
			2 `" "Kernel:" "Epanechnikov" "' ///
			3 `" "Kernel:" "Uniform" "' ///
			4 `" "BW:" "CER (common)" "' ///
			5 `" "Cluster" "by district" "' ///
			6 `" "No" "FEs" "' ///
			7 `" "District" "FEs" "' ///
			8 `" "With pop" "mismatches" "' ///
			, labsize(vsmall)) ///
	legend(order(2 "Commercial power, preferred   " 6 "Brightness, preferred   " ///
				 4 "Commercial power, sens   " 8 "Brightness, sens") c(4) pos(6) size(vsmall)) ///
	aspect(0.25)

graph export "$texfig/rd_fuzzy_coeffs_sens_cons.pdf", replace 

***

use "$results/RDROBUST_outcomes_fuzzy_shrug.dta", clear
keep if fuzzyvar=="lights_max2011_hat" | fuzzyvar=="vdp_pwr_h_com_avg_11"
keep if yvar=="ln_secc11_rural_cons_pc"
keep if polynomial==1
keep if word(ifs,1)=="in_fs_sample==1"

gen c = .
replace c = 1 if ifs=="in_fs_sample==1 & pop_mismatch20==0 & lights_diff<20" & fuzzy_step==1
replace c = 2 if kernel=="Epanechnikov" 
replace c = 3 if kernel=="Uniform"
replace c = 4 if bwmethod=="cerrd"
replace c = 5 if vce=="vce(cluster stdt)"
replace c = 6 if fe==""
replace c = 7 if fe=="DTFEfs*"
replace c = 8 if ftag=="popmismatch"
keep if c!=.
unique c
assert r(unique)==8 & r(N)==16
replace c = c - 0.07 if fuzzyvar=="vdp_pwr_h_com_avg_11"
replace c = c + 0.07 if fuzzyvar=="lights_max2011_hat"

foreach v of varlist beta_robust lci_robust uci_robust {
	replace `v' = `v'*10 if fuzzyvar=="vdp_pwr_h_com_avg_11"
	replace `v' = `v'*3 if fuzzyvar=="lights_max2011_hat"
}

twoway ///
	(rspike uci_robust lci_robust c if inrange(c,0.5,1.5) & fuzzyvar=="vdp_pwr_h_com_avg_11", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust c if inrange(c,0.5,1.5) & fuzzyvar=="vdp_pwr_h_com_avg_11", msize(small) mc(midblue) msymbol(O)) ///
	(rspike uci_robust lci_robust c if c>1.5 & fuzzyvar=="vdp_pwr_h_com_avg_11", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust c if c>1.5 & fuzzyvar=="vdp_pwr_h_com_avg_11", msize(small) mlc(navy) mfc(white) msymbol(O)) ///
	(rspike uci_robust lci_robust c if inrange(c,0.5,1.5) & fuzzyvar=="lights_max2011_hat", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust c if inrange(c,0.5,1.5) & fuzzyvar=="lights_max2011_hat", msize(small) mc(midblue) msymbol(T)) ///
	(rspike uci_robust lci_robust c if c>1.5 & fuzzyvar=="lights_max2011_hat", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust c if c>1.5 & fuzzyvar=="lights_max2011_hat", msize(small) mlc(navy) mfc(white) msymbol(T)) ///
	, ///
	yline(0, lcolor(black) lw(thin)) /// 
	ytitle("Scaled-up RD coeff (95% CI)", size(vsmall)) yscale(titlegap(*-2)) xtitle("") ///
	title("Outcome: log(2011 expenditure per capita)", color(black) size(vsmall)) /// 
	graphregion(lcolor(white)) graphregion(color(white)) plotregion(fcolor(white)) ///
	ylabel(-1(0.25)0.5,nogrid angle(0) labsize(vsmall)) /// yscale(range(-0.125 0.4)) ///
	xlabel( 1 `" "Preferred" "'  ///
			2 `" "Kernel:" "Epanechnikov" "' ///
			3 `" "Kernel:" "Uniform" "' ///
			4 `" "BW:" "CER (common)" "' ///
			5 `" "Cluster" "by district" "' ///
			6 `" "No" "FEs" "' ///
			7 `" "District" "FEs" "' ///
			8 `" "With pop" "mismatches" "' ///
			, labsize(vsmall)) ///
	legend(order(2 "Commercial power, preferred   " 6 "Brightness, preferred   " ///
				 4 "Commercial power, sens   " 8 "Brightness, sens") c(4) pos(6) size(vsmall)) ///
	aspect(0.25)

graph export "$texfig/rd_fuzzy_coeffs_sens_ln_cons.pdf", replace 




   // MAKE FIGURE
file open tablewrite using "$texfig/figure_rd_fuzzy_shrug_sens.tex", write text replace
 
file write tablewrite "\begin{figure}[h!]\centering" _n
file write tablewrite "\caption{Sensitivities for village-level fuzzy RD in expenditure per capita}" _n
file write tablewrite "\label{fig:rd_fuzzy_shrug_sens}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "{\includegraphics[width=\textwidth, trim={2mm 24mm 1mm 23mm}, clip]{${texfig_short}/rd_fuzzy_coeffs_sens_cons.pdf}} " _n
file write tablewrite "{\includegraphics[width=\textwidth, trim={2mm 24mm 1mm 21mm}, clip]{${texfig_short}/rd_fuzzy_coeffs_sens_ln_cons.pdf}} " _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- Each panel reports fuzzy RD sensitivity analysis for expenditure outcomes " _n
file write tablewrite "in Table \ref*{tab:rd_fuzzy_shrug}. Circles plot fuzzy RD point estimates using  " _n
file write tablewrite "hours of commercial power as the endogenous \`\`treatment'' variable, scaled up by a factor of 10. " _n
file write tablewrite "Triangles plot fuzzy RD point estimates using nighttime brightness as the endogenous \`\`treatment'' variable, " _n
file write tablewrite "scaled up by a factor of 2.6. Whiskers denote (scaled-up) 95\% confidence intervals." _n
file write tablewrite "Solid markers reproduce our preferred estimates from Table \ref*{tab:rd_fuzzy_shrug}, while hollow markers report sensitivities. " _n
file write tablewrite "First, we compare Epanechnikov and uniform weighting kernels to our preferred triangular kernel."  _n
file write tablewrite "Next, we compare our preferred MSE-optimal bandwidth selection algorithm ({\tt mserd}) with the " _n
file write tablewrite "coverage error rate optimal bandwidth algorithm ({\tt cerrd}). " _n
file write tablewrite "Next, we test alternative standard errors, clustering by district. " _n
file write tablewrite "Next, we remove state fixed effects, and then add district fixed effects. " _n
file write tablewrite "Finally, we add in the 11\% of villages with population discrepancies in the merged habitation data. " _n 
file write tablewrite "}" _n
file write tablewrite "\end{figure}" _n

file close tablewrite

}

********************************************************************************
********************************************************************************

